package com.rdc.qingkou.model;

import android.util.Log;

import com.rdc.qingkou.contract.UserInfoContract;
import com.rdc.qingkou.entity.ModifyUserInfoResult;
import com.rdc.qingkou.model.imodel.OnHttpCallBack;
import com.rdc.qingkou.retrofitService.IPersonalSettingsService;
import com.rdc.qingkou.utils.MyUtils;

import java.io.File;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by WaxBerry on 2017/11/12.
 */

public class UserInfoModel implements UserInfoContract.IModel{

    @Override
    public void getUserInfo(Integer userId, final OnHttpCallBack<ModifyUserInfoResult> callBack) {

        MyUtils.getRetrofitProvider("")
                .create(IPersonalSettingsService.class)
                .getUserInfo(userId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(new Observer<ModifyUserInfoResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ModifyUserInfoResult modifyUserInfoResult) {
                        callBack.onSuccessful(modifyUserInfoResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.onFaild(e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void modifyUserInfo(File photo, int sex, String nickName, String signature, Integer userId, final OnHttpCallBack<ModifyUserInfoResult> callBack) {

        MultipartBody.Part mediabody = null;

        if (photo!=null){

            RequestBody MeidaFile = RequestBody.create(MediaType.parse("multipart/form-data"), photo);
            //mediabody = MultipartBody.Part.createFormData("photo", photo.getName(), MeidaFile);  // image : null
            //mediabody = MultipartBody.Part.createFormData("media", photo.getName(), MeidaFile);   // image : null
            mediabody = MultipartBody.Part.createFormData("image", photo.getName(), MeidaFile);   //okhttp : 500  com.jakewharton.retrofit2.adapter.rxjava2.HttpException: HTTP 500

            Log.d("UserInfoModel","MEDIABODY NAME: "+photo.getName());
        }else {
            mediabody = null;
            Log.d("UserInfoModel","MEDIABODY IS NULL");
        }


//        MyUtils.getRetrofitProvider2("")
        MyUtils.getRetrofitProvider("")

                .create(IPersonalSettingsService.class)
                .modifyUserInfo(mediabody,sex,nickName,signature,userId)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeOn(Schedulers.newThread())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ModifyUserInfoResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ModifyUserInfoResult modifyUserInfoResult) {
                        callBack.onSuccessful(modifyUserInfoResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.onFaild(e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
